<?php
require_once '../../ptnhuan-web1/libraries/utilites/functions.php';
$path = "../../ptnhuan-web1/data/films.txt";
$full_path = realpath($path);
$films = mreadFile($path);
$rating = NULL;
?>
<form method="POST" align="center">
    <input type="text" name="rating">
    <input type="submit" name="submit" value="Search">
</form>
<?php if (isset($_POST['submit'])): ?>
    <?php $rating = (double) $_POST['rating']; ?>
<?php endif; ?>
<?php foreach ($films as $film): ?>
    <?php $film = trim($film);
    if (!empty($film)):
        ?>
        <?php
        $mfil = explode(";", $film);
        $mfile = trim($mfil[1]);
        ?>
        <?php if (empty($rating)): ?>
            <?php if (isset($mfil[2]) || isset($mfil[3])): ?>
                <img src = '../../ptnhuan-web1/public/images/films/<?php echo $mfile ?>'> 
                <?php echo $mfil[2] ?>
                <?php echo $mfil[3] ?>
                <a href="edit.php?if=<?php echo $mfil[0]; ?>">edit</a>
                <br>
            <?php endif; ?>
        <?php else: ?>
            <?php if (isset($mfil[2]) || isset($mfil[3])): ?>
                <?php if ($rating <= (double) $mfil[3]): ?>
                    <img src = '../../ptnhuan-web1/public/images/films/<?php echo $mfile ?>'> 
                    <?php echo $mfil[2] ?>
                    <?php if (!empty($mfil[3])) echo $mfil[3] ?>
                    <a href="edit.php?if=<?php echo $mfil[0]; ?>">edit</a>
                    <br>
                <?php endif; ?>
            <?php endif; ?>
        <?php endif; ?>
    <?php endif; ?>
<?php endforeach; ?>